<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 依赖递归
    // 当前函数的结果依赖于前一个,两个,多个函数的结果

    // 求1-5的和
    // sum(5) = 1 + 2 + 3 + 4 + 5;    sum(5) = 5 + sum(4)
    // sum(4) = 1 + 2 + 3 + 4;        sum(4) = 4 + sum(3)
    // sum(3) = 1 + 2 + 3;            sum(3) = 3 + sum(2)
    // sum(2) = 1 + 2;                sum(2) = 2 + sum(1)
    // sum(1) = 1;                    sum(1) = 1;


    function sum(n) {
        if (n == 1) {
            return 1;
        }
        return n + sum(n - 1);
    }

    // sum(5) => 5 + sum(4) => 4 + sum(3) => 3 + sum(2) => 2+ sum(1)
    //                                                           |
    //   15   <=   5  +  10  <= 4 +    6  <=  3 +    3  <=   2+   1   


    // debugger;
    var res = sum(100000);
    console.log(res);

</script>

</html>